package library

import (
	"context"
	"encoding/json"

	"go-mall/api/request"
	"go-mall/common/logger"
	"go-mall/common/util/httptool"
)

type DemoLib struct {
	ctx context.Context
}

func NewDemoLib(ctx context.Context) *DemoLib {
	return &DemoLib{ctx: ctx}
}

type OrderCreateResult struct {
	UserId    int64  `json:"user_id"`
	BillMoney int64  `json:"bill_money"`
	OrderNo   string `json:"order_no"`
	State     int8   `json:"state"`
	PaidAt    string `json:"paid_at"`
	CreatedAt string `json:"created_at"`
	UpdatedAt string `json:"updated_at"`
}

func (lib *DemoLib) TestPostCreateOrder() (*OrderCreateResult, error) {
	data := request.DemoOrderCreate{
		UserId:       12345,
		BillMoney:    20,
		OrderGoodsId: 11110,
	}

	jsonReq, _ := json.Marshal(data)
	httpCode, respBody, err := httptool.Post(lib.ctx,
		"http://localhost/building/create-demo-order", jsonReq)
	logger.New(lib.ctx).Info("create-demo-order api response ",
		"code", httpCode,
		"data", respBody,
		"err", err,
	)
	if err != nil {
		return nil, err
	}

	reply := &struct {
		Code int `json:"code"`
		Data *OrderCreateResult
	}{}
	json.Unmarshal(respBody, reply)
	return reply.Data, nil
}
